package cn.linpq.event;

import cn.linpq.label.MineLabel;
import cn.linpq.panel.MineField;

import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Desc : 类块监听类 使用继承MouseAdapter代替MouseListener
 * 此处使用了适配器模式
 * ClassName : cn.linpq.event.MineLabelMouseListenter
 *
 * @author : 林鹏群
 * @date : 2022/12/12 11:43
 */
public class MineLabelMouseListenter extends MouseAdapter {
    private MineField mineField;
    private boolean first = true;

    public MineLabelMouseListenter(MineField mineField) {
        this.mineField = mineField;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //获取事件源
        MineLabel source = (MineLabel) e.getSource();
        //双键按下
        if (InputEvent.BUTTON1_DOWN_MASK + InputEvent.BUTTON3_DOWN_MASK == e.getModifiersEx()) {
            mineField.pressMineAround(source);
            source.setDoubleClick(true);
        } else if (InputEvent.BUTTON1_MASK == e.getModifiers()) {
            //左键按下[1、如果第一次，部署雷]
            if (first) {
                mineField.buildMines(source);
            }
            mineField.leftPress(source);
        }else if(InputEvent.BUTTON3_MASK == e.getModifiers()){
            //右键按下
            mineField.rightClick(source);
        }


    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //获取事件源
        MineLabel source = (MineLabel) e.getSource();

        //双键按下
        if (source.isDoubleClick()) {
            mineField.openMineAround(source);
            source.setDoubleClick(false);
        } else if (InputEvent.BUTTON1_MASK == e.getModifiers()) {
        //左键按下[1、如果第一次，启动定时器；2、展开雷块；3、判断胜率/判断失败；]
            if (first) {
                first = false;
                mineField.getMineFrame().getMineState().getTimer().start();
            }
            mineField.openMineLabel(source);
        }else if(InputEvent.BUTTON3_MASK == e.getModifiers()){
        //右键按下
        }

    }
}
